package com.example.bili.view.activity;

import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.bili.R;
import com.example.bili.adapter.homepageAdaper;
import com.example.bili.allbean.MyinfoBean;
import com.example.bili.base.BaseActivity;
import com.example.bili.module.OkHttp;
import com.example.bili.view.fragment.D_DrawLayout;
import com.example.bili.view.fragment.focusonpage;
import com.example.bili.view.fragment.foundpage;
import com.example.bili.view.fragment.hisplaypage;
import com.example.bili.view.fragment.livepage;
import com.example.bili.view.fragment.partitionpage;
import com.example.bili.view.fragment.recommendedpage;
import com.facebook.drawee.view.SimpleDraweeView;
import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Request;

import static com.example.bili.internet.connectors.MYINFO;

/**
 * Created by Ad   ministrator on 2017/3/8.mikikm,l 首页
 */

public class Homepage extends BaseActivity implements View.OnClickListener, OkHttp.DataCallBack {
    private ViewPager viewPager;
    private SlidingTabLayout slidingTabLayout;
    private ArrayList<Fragment> fragmentArrayList;
    private FrameLayout frameLayout;
    private LinearLayout topmenu;
    private DrawerLayout drawerLayout;
    private SimpleDraweeView face;
    private TextView name;

    @Override
    protected void initview() {
        viewPager = (ViewPager) findViewById(R.id.homepage_vp);
        slidingTabLayout = (SlidingTabLayout) findViewById(R.id.homepage_commontablayout);
        frameLayout = (FrameLayout) findViewById(R.id.homepage_leftdrawlayout_fl);
        drawerLayout = (DrawerLayout) findViewById(R.id.homepage_dl);
        face = (SimpleDraweeView) findViewById(R.id.homepage_topmenu_myimg_img);
        name = (TextView) findViewById(R.id.homepage_topmenu_myname_tv);
        topmenu = (LinearLayout) findViewById(R.id.homepage_topmenu_left_lin);
        topmenu.setOnClickListener(this);

        fragmentArrayList = new ArrayList<>();
    }

    @Override
    public int getLayoutView() {
        return R.layout.homepage;
    }

    @SuppressLint("NewApi")
    @Override
    protected void m_oncreate() {
        //获取page页面
        getfragmentpage();
        //添加适配
        viewPager.setAdapter(new homepageAdaper(getSupportFragmentManager(), fragmentArrayList));
        viewPager.setCurrentItem(1);
        //关联viewpager
        slidingTabLayout.setViewPager(viewPager, new String[]{"直播", "推荐", "番剧", "分区", "关注", "发现"});

        //添加侧拉页面
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.homepage_leftdrawlayout_fl, new D_DrawLayout());
        fragmentTransaction.commit();
        //请求个人信息
        Map<String, String> map = new HashMap<>();
        map.put("mid", "775");
        OkHttp.postAsync(MYINFO, map, this);
    }


    public void getfragmentpage() {
        //直播
        fragmentArrayList.add(new livepage());
        //首页
        fragmentArrayList.add(new recommendedpage());
        //番剧
        fragmentArrayList.add(new hisplaypage());
        //分区
        fragmentArrayList.add(new partitionpage());
        //关注
        fragmentArrayList.add(new focusonpage());
        //发现
        fragmentArrayList.add(new foundpage());
    }

    @Override
    public void onClick(View view) {
        drawerLayout.openDrawer(Gravity.START);
    }

    @Override
    public void requestFailure(Request request, IOException e) {

    }

    @Override
    public void requestSuccess(String result) throws Exception {
        MyinfoBean myinfoBean = new Gson().fromJson(result, MyinfoBean.class);
        face.setImageURI(myinfoBean.getCard().getFace());
        name.setText(myinfoBean.getCard().getName());
    }
}
